view and Manipulate-Files

Viewing files and Text Manipulation

Introduction:

  • In Linux nearly everything u deal with directly is a file and most importantly configuration files.
  • All Configuration files are treated as Text file we open them and Manupilate/change them as we need.

for illustration i will use snort config file to view and manipulate

sudo apt install snort

Viewing files:

  • cat is most common and basic way to view a file, however it has limitations.

  • this will display all config file of snort.
    19-56-Linux-23_Jul_2025.png


head and tail commands:

  • rather than using cat we can use these options (head,tail) to display a part of the content in a file, by default they display 10 lines content of a file.
  • Dont forget to visit there manual tail --help or man head to find more options.
  • u can to many complex funtion unlike the using cat.
  • head: Beginning content of a file.
  • tail Ending content of a file.
    20-26-Linux-23_Jul_2025.png

head: -(number of lines switch)

  • if u wanna view the specific amount content we use -(n) switch
  • first 20 lines example. head -20 /etc/snort/snort.lua

tail: -(number of lines switch)

  • if u wanna view the specific amount content we use -(n) switch
  • last 20 lines example. tail -20 /etc/snort/snort.lua

20-43-Linux-23_Jul_2025.png


nl Numbering the Lines:

  • We can view the number of the lines by using nl command.
  • In this example this config file has 220 lines.
  • Keep that in mind that we use nlit skips all blank lines.

note: there is famous github in zsh shell u can add that can show number of lines and syntax highlighting of the code called batcat its just better version of cat but not do research about it before using it.

20-49-Linux-23_Jul_2025.png

20-55-Linux-23_Jul_2025.png

📄 Viewing Files with less and more

In Linux, you can use less and more to view the contents of a file in the terminal — especially useful for reading long files.

🔍 less

  • Think of less like reading a man page.
  • It shows one screen at a time, but you can scroll up and down using:
    • Arrow keys
    • Page Up / Page Down
    • Search with /
  • It's more powerful and flexible than more.

Example:

less filename.txt

📄 Viewing Files with less and more

  • In Linux, you can use less and more to view the contents of a file in the terminal especially useful for reading long files.
  • 📝 Tip: Use less whenever possible it’s more modern and user-friendly.

🔍 less

  • Think of less like reading a man page.
  • It shows one screen at a time, but you can scroll up and down using:
    • Arrow keys
    • Page Up / Page Down
    • Search with /
  • It's more powerful and flexible than more.

Example:

less filename.txt
		or
nl  /etc/snort/snort.lua | less 

23-01-Linux-23_Jul_2025.png
23-02-Linux-23_Jul_2025.png

📖 more

  • more displays the file page by page.
  • Press:
    • Space to go to the next page
    • Enter to go line by line
  • You can’t scroll back up, only forward.

Example:

more filename.txt
	  or
nl  /etc/snort/snort.lua | more 

23-06-Linux-23_Jul_2025.png

23-05-Linux-23_Jul_2025.png


File Manipulation

Utilizing grep for Text Manipulation and Viewing files:

  • for example we can achieve this by piping the output of nl view the file where occurance of a certain word,
  • ex: nl /etc/snort/snort.lua | grep configure

21-07-Linux-23_Jul_2025.png

lets see how complex when we can combine the head or tail, nl and cat with grep .

  • in this example i will use nl to output the file with number of lines and use grep to capture and shorten the output to display the first 4 lines the when statement in lua programming language this is what snort is currently is built with.

  • without combining it with head or tail.
    22-34-Linux-23_Jul_2025.png

  • combining it with head or tail:

cat  /etc/snort/snort.lua | grep when | head -4 | cat > test.txt | nl test.txt 
  • in this example we combined the commands using Piping( | ) to run these commands. I searched and viewed for when statement from snort.lua, take the first 4, save them to test.txt, then try to number them using nl.
    Remark: | (Pipe): Passes output of one command as input to another command.

22-49-Linux-23_Jul_2025.png

sed Command: The stream editor

01-14-Linux-24_Jul_2025.png

  • view manual about sed first i will cover only the basics of man sed or sed --help.

  • sed command can search for occurence of a word inside the File 📝 and preform an Action.

  • it can find and replace content of a file

  • lets work on this file test

    01-30-Linux-24_Jul_2025.png

cat  /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst | head -10 | cat > test &&  cat  test

  • if u dont have the wordlist file just copy this in a file.
!@#$%
!@#$%^
!@#$%^&
!@#$%^&*
!boerbul
!boerseun
!gatvol
!hotnot
!kak
!koedoe

Search in the file using sed:

  • sed -n /text-to-search/p
  • -n: Suppresses automatic printing — sed won’t show every line by default.
  • /text-to-search/: The pattern you want to search for.
  • p: Prints only the lines that match the pattern.

📝 With -n, sed shows only matching lines.

  • see the examples:

03-34-Linux-24_Jul_2025.png

Other Example of searching with sed using Pipe( | ):

03-18-Linux-24_Jul_2025.png

Manipulate a file content:

Syntax: echo "sed <flag1>/textiwanttochange/New-text-/<flag2> [filepath full path > newpath]"
flags to use to manipulate a file sand g
s == subtitute the text to new text
g == global - all occurance of the word repeated multiple types

  • added a multiple occurance to test the g flag of the word !hotnot

02-28-Linux-24_Jul_2025.png

  • Run the command
sed  s/hotnot/I-manipulated-you/g ~/test > test2

02-40-Linux-24_Jul_2025.png

Challenge

Before you move on Try to solve this challenge, try out the skill.you learned:

  • Navigate to /usr/share/metasploit-framework/data/wordlists. This is a,directory of multiple wordlists that can be used to brute force passwords.
    in various password-protected devices using Metasploit, the most popular pentesting and hacking framework.
  • Use the cat command to view the contents of the file password.lst.,
  • Use the more command to display the file password.lst.,
  • Use the less command to view the file password.lst.,
  • Now use the nl command to place line numbers on the passwords in,password.lst. There should be around 88,396 passwords.
  • Use the tail command to see the last 20 passwords in password.lst.,
  • Use the cat command to display password.Ist and pipe it to find all the passwords that contain 123.

finding wordlist since the directory of the wordlist changed.

sudo find / -type f -name "*word.lst"
00-14-Linux-24_Jul_2025.png

cat or nl
nl /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst

00-22-Linux-24_Jul_2025.png

  • finding the last 20 passwords:
    added more line of command save it in a file and show the output
    nl /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst | tail -20 | cat > test && nl test

00-38-Linux-24_Jul_2025.png

finding the passwords with 123 in them

  • Note:When you're searching for something like numbers using grep, avoid using nl (which adds line numbers), because it can interfere with your search results. The line numbers added by nl might confuse grep and give you inaccurate matches. look in the above Screenshot.👆

  • cat /opt/metasploit-framework/embedded/framework/data/wordlists/password.lst | grep 123

  • Instead, use cat to display the file contents and pipe it directly to grep, like this:

00-43-Linux-24_Jul_2025.png